AddTaskController   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 26
dl 0
loc 27
c 0
b 0
f 0
rs 10

2 Functions

Rating   Name   Duplication   Size   Complexity  
A post 0 11 2
A get 0 6 1
1
import {
2
  BadRequestException,
3
  Body,
4
  Controller,
5
  Get,
6
  Inject,
7
  Post,
8
  Render,
9
  Res,
10
  UseGuards
11
} from '@nestjs/common';
12
import { Response } from 'express';
13
import { ICommandBus } from 'src/Application/ICommandBus';
14
import { IsAuthenticatedGuard } from 'src/Infrastructure/HumanResource/User/Security/IsAuthenticatedGuard';
15
import { WithName } from 'src/Infrastructure/Common/ExtendedRouting/WithName';
16
import { CreateTaskCommand } from 'src/Application/Task/Command/CreateTaskCommand';
17
import { TaskDTO } from '../DTO/TaskDTO';
18
import { RouteNameResolver } from 'src/Infrastructure/Common/ExtendedRouting/RouteNameResolver';
19
20
@Controller('app/tasks/add')
21
@UseGuards(IsAuthenticatedGuard)
22
export class AddTaskController {
23
  constructor(
24
    @Inject('ICommandBus')
25
    private readonly commandBus: ICommandBus,
26
    private readonly resolver: RouteNameResolver
27
  ) {}
28
29
  @Get()
30
  @WithName('crm_tasks_add')
31
  @Render('pages/tasks/add.njk')
32
  public async get() {
33
    return {};
34
  }
35
36
  @Post()
37
  public async post(@Body() taskDto: TaskDTO, @Res() res: Response) {
38
    const { name } = taskDto;
39
40
    try {
41
      await this.commandBus.execute(new CreateTaskCommand(name));
42
43
      res.redirect(303, this.resolver.resolve('crm_tasks_list'));
44
    } catch (e) {
45
      throw new BadRequestException(e.message);
46
    }
47
  }
48
}
49